use std::io::BufWriter;

use fetch::fetch_training;

use super::*;

#[derive(Parser, Clone, Debug)]
pub struct FetchTrainingArgs {
    #[clap(short, long)]
    id: u32,
    #[clap(short, long)]
    out: PathBuf,
}

impl FetchTrainingArgs {
    pub async fn run(self, _common: &CommonArgs) -> eyre::Result<()> {
        let FetchTrainingArgs { id, out } = self;
        let training = fetch_training(id).await?;
        let training = Training { training };
        let file = fs::File::create(out)?;
        serde_json::to_writer_pretty(BufWriter::new(file), &training)?;
        Ok(())
    }
}
